package com.example.boxugu.myapplication.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.example.boxugu.myapplication.Fragment.MyFragment;
import com.example.boxugu.myapplication.Moder.ChangeUserInfoModer;
import com.example.boxugu.myapplication.Moder.UserInfoModer;
import com.example.boxugu.myapplication.R;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class ChangeUserinfoActivity extends AppCompatActivity {
    ImageButton imageButton;
    EditText musername,mnickname,mgrade,mdes;
    Button button;
    String token;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_userinfo);
        imageButton=findViewById(R.id.imgbtn_back);
        musername=findViewById(R.id.yhmtv);
        mnickname=findViewById(R.id.nametv);
        mgrade=findViewById(R.id.sextv);
        mdes=findViewById(R.id.qmtv);
        button=findViewById(R.id.save_btn);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(ChangeUserinfoActivity.this, MainActivity.class);
                startActivity(intent);
                finish();

            }
        });
        final SharedPreferences sharedPreferences=ChangeUserinfoActivity.this.getSharedPreferences("boxugu", Context.MODE_PRIVATE);
        token=sharedPreferences.getString("token","");
        if (token.length()>0) {
            RequestParams params = new RequestParams("http://148.70.46.9:8080/boxuegu/bxg/getuserinfo");
            params.addBodyParameter("token", token);
            x.http().post(params, new Callback.CacheCallback<String>() {
                @SuppressLint("ResourceType")
                @Override
                public void onSuccess(String result) {
                    Gson gson = new Gson();
                    UserInfoModer userInfoModer = gson.fromJson(result, UserInfoModer.class);
                    if (userInfoModer != null && userInfoModer.data != null) {
                        musername.setText(userInfoModer.data.username);
                        mnickname.setText(userInfoModer.data.nickname);
                        mgrade.setText(userInfoModer.data.grade);
                        mdes.setText(userInfoModer.data.des);
                    }


                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {


                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }

                @Override
                public boolean onCache(String result) {
                    return false;
                }
            });
        }
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (token.length()>0) {
                    RequestParams params = new RequestParams("http://148.70.46.9:8080/boxuegu/bxg/changeuserinfo");
                    params.addBodyParameter("token", token);
                    String nickname=mnickname.getText().toString();
                    String grade=mgrade.getText().toString();
                    String des=mdes.getText().toString();
                    params.addBodyParameter("nickname",nickname);
                    params.addBodyParameter("grade",grade);
                    params.addBodyParameter("des",des);
                    x.http().post(params, new Callback.CacheCallback<String>() {
                        @SuppressLint("ResourceType")
                        @Override
                        public void onSuccess(String result) {
                            Gson gson = new Gson();
                            ChangeUserInfoModer changeUserInfoModer = gson.fromJson(result, ChangeUserInfoModer.class);
                            Log.i("changeUserInfoModer","onsuccsss");
                            if (changeUserInfoModer != null && changeUserInfoModer.msg != null) {
                                if (changeUserInfoModer.code==0){
                                    Toast.makeText(ChangeUserinfoActivity.this,"修改成功",Toast.LENGTH_LONG).show();
                                    SharedPreferences sp = getSharedPreferences("boxugu", MODE_PRIVATE);
                                    SharedPreferences.Editor editor=sp.edit();
                                    editor.commit();
                                    Intent intent=new Intent(ChangeUserinfoActivity.this, MainActivity.class);
                                    startActivity(intent);
                                    finish();


                                }else {
                                    Toast.makeText(ChangeUserinfoActivity.this,"修改失败",Toast.LENGTH_LONG).show();
                                }

                            }


                        }

                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {


                        }

                        @Override
                        public void onCancelled(CancelledException cex) {

                        }

                        @Override
                        public void onFinished() {

                        }

                        @Override
                        public boolean onCache(String result) {
                            return false;
                        }
                    });
                }

            }
        });

    }
}
